//
//  AppDelegate.m
//  youmengdemo
//
//  Created by 张乐乐 on 2020/6/28.
//  Copyright © 2020 taohuikeji. All rights reserved.
//

#import "AppDelegate.h"
#import <UMCommon/UMCommon.h>
#import <UMShare/UMShare.h>
#import <UMCommonLog/UMCommonLogHeaders.h>
#import "WXApi.h"
#import "WXApiManager.h"

static NSString *youmengAppKey = @"5cdcff060cafb27efb0011c3";

static NSString *weixinAppKey = @"wxbb801367d84736cd";
static NSString *weixinAppSecret = @"5661f18b060f4863e37a704eb8f4aaa6";
static NSString *weixinUniversalLink = @"https://www.taohuikeji.net/NewTLH/";

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//        初始化微信
    [self initWXSDK];
    //    初始化友盟
    [self initUMSDK];
    return YES;
}

//初始化微信
-(void)initWXSDK{
    //在register之前打开log, 后续可以根据log排查问题
    [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString *log) {
        NSLog(@"WeChatSDK: %@", log);
    }];

    [WXApi registerApp:weixinAppKey universalLink:weixinUniversalLink];
    
    //调用自检函数
    [WXApi checkUniversalLinkReady:^(WXULCheckStep step, WXCheckULStepResult* result) {
        NSLog(@"hhhh-->>%@, %u, %@, LLL-->>%@", @(step), result.success, result.errorInfo, result.suggestion);
    }];
}

//初始化友盟
- (void)initUMSDK{
    [UMCommonLogManager setUpUMCommonLogManager];
    [UMConfigure setLogEnabled:YES];
    [UMConfigure initWithAppkey:youmengAppKey channel:@"App Store"];
    
    //配置微信平台的Universal Links
    //微信和QQ完整版会校验合法的universalLink，不设置会在初始化平台失败
    [UMSocialGlobal shareInstance].universalLinkDic = @{@(UMSocialPlatformType_WechatSession):weixinUniversalLink};
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:weixinAppKey appSecret:weixinAppSecret redirectURL:@"http://mobile.umeng.com/social"];
    /*设置小程序回调app的回调*/
    [[UMSocialManager defaultManager] setLauchFromPlatform:(UMSocialPlatformType_WechatSession) completion:^(id userInfoResponse, NSError *error) {
        NSLog(@"setLauchFromPlatform:userInfoResponse:%@",userInfoResponse);
    }];
}

// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
//         // 其他如支付等SDK的回调
//        if ([url.host isEqualToString:@"pay"]) {
//            return [WXApi handleOpenURL:url delegate:self];
//        }
    }
    return result;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
//        // 其他如支付等SDK的回调
//        if ([url.host isEqualToString:@"pay"]) {
//            return [WXApi handleOpenURL:url delegate:self];
//        }
    }
    return result;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
//        // 其他如支付等SDK的回调
//        if ([url.host isEqhandleOpenUniversalLinkualToString:@"pay"]) {
//            return [WXApi handleOpenURL:url delegate:self];
//        }
    }
    return result;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler
{
    if (![[UMSocialManager defaultManager] handleUniversalLink:userActivity options:nil]) {
        // 其他SDK的回调

        BOOL haha = [WXApi handleOpenUniversalLink:userActivity delegate:[WXApiManager sharedManager]];
        return haha;
    }
    return YES;

}


@end
